public class code3 {
    public int maxProfit(int[] prices, int fee) {
        int n = prices.length;

        int[] f = new int[n];//持有股票
        int[] g = new int[n];//不持有股票

        f[0] = -prices[0];

        for(int i=1;i<n;i++) {
            f[i] = Math.max(f[i-1],g[i-1]-prices[i]);
            g[i] = Math.max(g[i-1],f[i-1]+prices[i]-fee);//注意买入和卖出属于一次交易，只需要付一次手续费
        }

        return g[n-1];
    }
}
